package com.lottery.im.module.main.ui;

import android.content.Intent;
import android.view.View;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.base.BaseActivity;
import com.lottery.im.constant.SpKeyConstant;
import com.lottery.im.module.main.presenter.VerPwdInputPresenter;
import com.lottery.im.module.main.view.VerPwdInputView;
import com.lottery.im.module.tim.business.LoginBusiness;
import com.lottery.im.util.AppManagerUtil;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.widget.dialog.ConfirmWarnDialogBuilder;
import com.lottery.im.widget.vercodeedittext.VerificationAction;
import com.lottery.im.widget.vercodeedittext.VerificationCodeEditText;
import com.tencent.imsdk.TIMCallBack;

/*
 * 验证密码输入界面
 * */
public class VerPwdInputActivity extends BaseActivity<VerPwdInputPresenter> implements
    VerPwdInputView {

  @BindView(R.id.et_pwd) VerificationCodeEditText mEtPwd;

  @Override protected int getLayoutResId() {
    return R.layout.activity_ver_pwd_input;
  }

  @Override protected void initPresenter() {
    presenter = new VerPwdInputPresenter(this, this, this);
    presenter.init();
  }

  @Override public void initView() {

    StatusBarHelper.translucent(this);
    StatusBarHelper.setStatusBarLightMode(this);
    mEtPwd.requestFocus();
    mEtPwd.setOnVerificationCodeChangedListener(
        new VerificationAction.OnVerificationCodeChangedListener() {
          @Override public void onVerCodeChanged(CharSequence s, int start, int before, int count) {

          }

          @Override public void onInputCompleted(CharSequence s) {
            hideKeyBoard();
            presenter.checkEquipmentPwd(s.toString());
          }
        });
  }

  @OnClick(R.id.btn_back) public void onViewClicked() {
    giveupVer();
  }


  private void giveupVer() {

    ConfirmWarnDialogBuilder.getInstance(this).setContent("确定放弃登录验证，重新登录？")
        .setCancel("取消",null)
        .setConfirm("确定", v -> LoginBusiness.logout(new TIMCallBack() {
          @Override public void onError(int i, String s) {
          }

          @Override public void onSuccess() {
            SPUtils.put(mActivity, SpKeyConstant.TOKEN_KEY, "");
            SPUtils.put(mActivity, SpKeyConstant.USERINFO_KEY, "");
            AppManagerUtil.getAppManager().finishAllActivity();
            startActivity(new Intent(mActivity, SplashActivity.class));
          }
        })).show();
  }


  @Override public void onBackPressed() {
    giveupVer();
  }

  @Override public void checkEquipmentPwdError() {
    mEtPwd.setText("");
    showKeyBoard();
  }

  @Override public void checkEquipmentPwdSuce() {
    finish();
  }



}
